<template>
    <el-container class="home-container">
      <!--  头部区域  -->
      <el-header>
        <div>
          <img src="../../assets/logo.png" alt="" class="header-img">
          <span>后台管理系统</span>
        </div>
        <el-button @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <!--  侧边栏区域  -->
      <el-aside :width="isCollapse?'64px' : '200px'">
        <div class="toggle-out">
          <i :class="[isCollapse ?'el-icon-s-unfold' : 'el-icon-s-fold' , 'toggle-button']" @click="toggleCollapse"></i>
        </div>
        <!--  侧边栏菜单区域  -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath" >
          <!--  一级菜单  -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!--  一级菜单模板区域  -->
            <template slot="title">
              <!--  图标  -->
              <i :class="iconObj[item.id]"></i>
              <!--  文本  -->
              <span>{{item.name}}</span>
            </template>
            <!--  二级菜单  -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <!--  图标  -->
                <i class="el-icon-menu"></i>
                <!--  文本  -->
                <span>{{subItem.name}}</span>
            </template>
            </el-menu-item>
         </el-submenu>
        </el-menu>
      </el-aside>
      <!--  右侧主体区域  -->
      <el-main>
        <router-view></router-view>
      </el-main>
      </el-container>
    </el-container>
</template>
<script>
import { getMenus } from '../../api/user'
export default {
  data () {
    return {
      menuList: [],
      iconObj: {
        1: 'iconfont iconyonghuguanli',
        2: 'iconfont iconquanxianguanli',
        3: 'iconfont iconshangpinguanli',
        4: 'iconfont icondingdanguanli',
        5: 'iconfont iconshujutongji'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    getMenus () {
      getMenus(this.loginForm).then(response => {
        const { data } = response
        this.menuList = data.data
      }).catch(error => {
        this.$message.error(error)
      })
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    async logout () {
      const confirmResult = await this.$confirm('确定退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      console.log(confirmResult)
      if (confirmResult === 'confirm') {
        this.$store.dispatch('user/logout').then(() => {
          this.$router.push({ path: '/login' })
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  color: white;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.header-img{
  width: 35px;
  height: 35px;
  border-radius: 25px;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}

.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}

.toggle-out{
  display: flex;
  justify-content: flex-end;
  color: white;
  font-size: 20px;
  padding-right: 20px;
}

.toggle-button {
   cursor: pointer;
}

</style>
